package view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controller.ClientController;
import controller.DatabaseConnection;
import controller.FeedController;
import controller.TirazhController;

public class MainWindow {

	private JFrame frame;
	private JTable table;
	private static JTextField textField;
	private static JTextField textField_1;
	private static JTextField textField_2;
	private static JTextField textField_3;
	private static JTextField txtId;
	private static JTextField textField_4;
	private static JTextField textField_5;
	private static JTextField textField_6;
	private static JTextField textField_7;
	private static JTextArea textArea;
	private static JTextField textField_8;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					MainWindow window = new MainWindow();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * 
	 * @throws Exception
	 */
	public MainWindow() throws Exception {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * 
	 * @throws Exception
	 */
	private void initialize() throws Exception {
		frame = new JFrame();
		frame.setBounds(100, 100, 878, 587);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 207, 837, 295);

		textField = new JTextField();
		textField.setBounds(10, 29, 107, 20);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(127, 29, 107, 20);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(10, 65, 107, 20);
		textField_2.setColumns(10);

		textField_3 = new JTextField();
		textField_3.setBounds(127, 65, 107, 20);
		textField_3.setColumns(10);

		JButton button = new JButton("База клиентов");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				ClientWindow.main(null);

				try {
					Connection conn = DatabaseConnection.getInstance()
							.getConnection();

					Statement st = conn.createStatement();
					ResultSet rs = st.executeQuery("SELECT * FROM clients");
					int numColumns = rs.getMetaData().getColumnCount();
					Vector column = new Vector<>();
					Vector data_DB = new Vector<>();

					for (int i = 1; i <= numColumns; i++) {
						column.add(rs.getMetaData().getColumnName(i));
						while (rs.next()) {
							Vector row = new Vector<>();
							for (int i2 = 1; i2 <= numColumns; i2++) {
								row.add(rs.getString(i2));
							}
							data_DB.add(row);
						}
					}
					rs.close();
					st.close();
					ClientWindow.table.setModel(new DefaultTableModel(data_DB,
							column));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		button.setBounds(10, 148, 137, 23);

		JButton button_1 = new JButton("Очистистить");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				textField_1.setText("");
				textField_2.setText("");
				textField_3.setText("");
				textField_4.setText("");
				textField_5.setText("");
				textField_6.setText("");
				textField_7.setText("");
				textField_8.setText("");
				textArea.setText("");
			}
		});
		button_1.setBounds(157, 148, 140, 23);

		JButton button_2 = new JButton("Добавить");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientController clientController = new ClientController();
				clientController.createClient();

				FeedController feedController = new FeedController();
				feedController.createFeed(clientController.id_client,
						clientController.discounter);

				try {
					Connection conn = DatabaseConnection.getInstance()
							.getConnection();

					Statement st = conn.createStatement();
					ResultSet rs = st
							.executeQuery("SELECT * FROM feed WHERE active=\'1\'");
					int numColumns = rs.getMetaData().getColumnCount();
					Vector column = new Vector<>();
					Vector data_DB = new Vector<>();

					for (int i = 1; i <= numColumns; i++) {
						column.add(rs.getMetaData().getColumnName(i));
						while (rs.next()) {
							Vector row = new Vector<>();
							for (int i2 = 1; i2 <= numColumns; i2++) {
								row.add(rs.getString(i2));
							}
							data_DB.add(row);
						}
					}
					rs.close();
					st.close();
					table.setModel(new DefaultTableModel(data_DB, column));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		button_2.setBounds(304, 148, 143, 23);

		txtId = new JTextField();
		txtId.setEditable(false);
		txtId.setBounds(10, 117, 140, 20);
		txtId.setColumns(10);

		textField_4 = new JTextField();
		textField_4.setEditable(false);
		textField_4.setBounds(157, 117, 140, 20);
		textField_4.setColumns(10);

		textField_5 = new JTextField();
		textField_5.setBounds(307, 117, 140, 20);
		textField_5.setColumns(10);

		textField_6 = new JTextField();
		textField_6.setBounds(457, 117, 140, 20);
		textField_6.setColumns(10);

		textField_7 = new JTextField();
		textField_7.setBounds(607, 117, 140, 20);
		textField_7.setColumns(10);

		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		Connection conn = DatabaseConnection.getInstance().getConnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("SELECT * FROM feed WHERE active=\'1\'");
		int numColumns = rs.getMetaData().getColumnCount();
		Vector column = new Vector<>();
		Vector data_DB = new Vector<>();

		for (int i = 1; i <= numColumns; i++) {
			column.add(rs.getMetaData().getColumnName(i));
			while (rs.next()) {
				Vector row = new Vector<>();
				for (int i2 = 1; i2 <= numColumns; i2++) {
					row.add(rs.getString(i2));
				}
				data_DB.add(row);
			}
		}
		rs.close();
		st.close();

		table.setModel(new DefaultTableModel(data_DB, column));
		scrollPane.setViewportView(table);

		JLabel label = new JLabel("Объявления в базе:");
		label.setBounds(10, 182, 121, 14);

		JButton button_3 = new JButton("Отчет");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ReportWindow.main(null);
			}
		});
		button_3.setBounds(8, 513, 89, 23);
		button_3.setVisible(false);

		JButton button_4 = new JButton("Сформировать тираж");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser saveChooser = new JFileChooser();
				saveChooser.setDialogTitle("Сохранить как");
				saveChooser.setCurrentDirectory(new File("."));
				if (saveChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
					File tirazhFile = saveChooser.getSelectedFile();
					TirazhController tirController = new TirazhController();
					try {
						tirController.generateTirazh(tirazhFile);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				Connection conn;
				try {
					conn = DatabaseConnection.getInstance().getConnection();
				
				Statement st = conn.createStatement();
				ResultSet rs = st.executeQuery("SELECT * FROM feed WHERE active=\'1\'");
				int numColumns = rs.getMetaData().getColumnCount();
				Vector column = new Vector<>();
				Vector data_DB = new Vector<>();

				for (int i = 1; i <= numColumns; i++) {
					column.add(rs.getMetaData().getColumnName(i));
					while (rs.next()) {
						Vector row = new Vector<>();
						for (int i2 = 1; i2 <= numColumns; i2++) {
							row.add(rs.getString(i2));
						}
						data_DB.add(row);
					}
				}
				rs.close();
				st.close();

				
				table.setModel(new DefaultTableModel(data_DB, column));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		button_4.setBounds(107, 513, 160, 23);
		frame.getContentPane().setLayout(null);
		frame.getContentPane().add(scrollPane);
		frame.getContentPane().add(textField);
		frame.getContentPane().add(textField_1);
		frame.getContentPane().add(textField_2);
		frame.getContentPane().add(textField_3);
		frame.getContentPane().add(button);
		frame.getContentPane().add(button_1);
		frame.getContentPane().add(button_2);
		frame.getContentPane().add(txtId);
		frame.getContentPane().add(textField_4);
		frame.getContentPane().add(textField_5);
		frame.getContentPane().add(textField_6);
		frame.getContentPane().add(textField_7);
		frame.getContentPane().add(label);
		frame.getContentPane().add(button_3);
		frame.getContentPane().add(button_4);

		JLabel label_1 = new JLabel("Фамилия:");
		label_1.setBounds(10, 11, 87, 14);
		frame.getContentPane().add(label_1);

		JLabel label_2 = new JLabel("Имя:");
		label_2.setBounds(127, 11, 46, 14);
		frame.getContentPane().add(label_2);

		JLabel label_3 = new JLabel("Отчество:");
		label_3.setBounds(10, 49, 62, 14);
		frame.getContentPane().add(label_3);

		JLabel label_4 = new JLabel("Телефон:");
		label_4.setBounds(127, 49, 62, 14);
		frame.getContentPane().add(label_4);

		JLabel lblNewLabel = new JLabel("Количество символов:");
		lblNewLabel.setBounds(10, 96, 140, 14);
		frame.getContentPane().add(lblNewLabel);

		JLabel label_5 = new JLabel("Стоимость:");
		label_5.setBounds(157, 96, 134, 14);
		frame.getContentPane().add(label_5);

		JLabel label_6 = new JLabel("Дублирование:");
		label_6.setBounds(307, 96, 140, 14);
		frame.getContentPane().add(label_6);

		JLabel label_7 = new JLabel("Количество ячеек:");
		label_7.setBounds(457, 96, 140, 14);
		frame.getContentPane().add(label_7);

		JLabel label_8 = new JLabel("Количество повторений:");
		label_8.setBounds(607, 96, 182, 14);
		frame.getContentPane().add(label_8);

		JLabel label_9 = new JLabel("Текст объявления:");
		label_9.setBounds(244, 11, 140, 14);
		frame.getContentPane().add(label_9);

		JLabel label_10 = new JLabel("Стоимость символа:");
		label_10.setBounds(457, 152, 140, 14);
		frame.getContentPane().add(label_10);

		textField_8 = new JTextField();
		textField_8.setBounds(607, 149, 140, 20);
		frame.getContentPane().add(textField_8);
		textField_8.setColumns(10);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(244, 29, 603, 56);
		frame.getContentPane().add(scrollPane_1);

		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		textArea.setLineWrap(true);
		textArea.setFont(new Font("Tahoma", Font.PLAIN, 11));
		textArea.setAutoscrolls(false);
	}

	public static void setTxtId(String text) {
		txtId.setText(text);
	}

	public static void setTextField_4(String text) {
		textField_4.setText(text);
	}

	public static void setTextField_6(String text) {
		textField_6.setText(text);
	}

	public static String getTextArea() {
		return textArea.getText();
	}

	public static String getTextField() {
		return textField.getText();
	}

	public static String getTextField_1() {
		return textField_1.getText();
	}

	public static String getTextField_2() {
		return textField_2.getText();
	}

	public static String getTextField_3() {
		return textField_3.getText();
	}

	public static String getTextField_5() {
		return textField_5.getText();
	}

	public static String getTextField_6() {
		return textField_6.getText();
	}

	public static String getTextField_7() {
		return textField_7.getText();
	}

	public static String getTextField_8() {
		return textField_8.getText();
	}
}
